package com.bootdo.per.controller;

import java.util.List;
import java.util.Map;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.per.domain.ManagerDO;
import com.bootdo.per.service.ManagerService;
import com.bootdo.common.config.Constant;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;

/**
 * 
 * 
 * @author readke
 * @email 1992lcg@163.com
 * @date 2018-06-05 17:51:30
 */
 
@Controller
@RequestMapping("/per/manager")
public class ManagerController {
	@Autowired
	private ManagerService managerService;
	
	@GetMapping()
	@RequiresPermissions("per:manager:manager")
	String Manager(){
	    return "per/manager/manager";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("per:manager:manager")
	public PageUtils list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);
		List<ManagerDO> managerList = managerService.list(query);
		int total = managerService.count(query);
		PageUtils pageUtils = new PageUtils(managerList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("per:manager:add")
	String add(){
	    return "per/manager/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("per:manager:edit")
	String edit(@PathVariable("id") Long id,Model model){
		ManagerDO manager = managerService.get(id);
		model.addAttribute("manager", manager);
	    return "per/manager/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("per:manager:add")
	public R save( ManagerDO manager){
		if(managerService.save(manager)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("per:manager:edit")
	public R update( ManagerDO manager){
		managerService.update(manager);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("per:manager:remove")
	public R remove( Long id){
		if(managerService.remove(id)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("per:manager:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		managerService.batchRemove(ids);
		return R.ok();
	}
	
	/**
	 * 打开考核功能 
	 */
	@ResponseBody
	@RequestMapping("/on")
	@RequiresPermissions("per:manager:on")
	public R on( ManagerDO manager){
		manager.setStatus(Constant.SCORE_MANAGE_ON);
		managerService.update(manager);
		return R.ok();
	}
	/**
	 * 关闭考核功能 
	 */
	@ResponseBody
	@RequestMapping("/off")
	@RequiresPermissions("per:manager:off")
	public R off( ManagerDO manager){
		manager.setStatus(Constant.SCORE_MANAGE_OFF);
		managerService.update(manager);
		return R.ok();
	}
}
